#!/usr/bin/python

from stocky.stocky import interface
import sys


def usage():
    usage =         " USAGE:  %s  [company_code]...\n" %sys.argv[0]
    usage = usage + "              company_code as * will print report for all\n"
    return usage

    
def reportHeader():
    header = ""
    header = header + "=================================================================================================\n"
    header = header + "   STOCK  |  (%)STOPLOSS  |  PURCH PRICE  | MARKET PRICE  | DECISION |    SELL AT    | GAIN/LOSS \n"  
    header = header + "================================================================================================="
    return header
    
        
def reportBody(stock):
    return "%10s %15s %15s %15s %10s %15.1f %10.1f" %(stock.company_code, stock.stoploss, \
                    stock.purchase_price, stock.curr_market_price, stock.curr_decision, \
                    stock.curr_selling_target, stock.gain_loss)


if len(sys.argv) > 1 and (sys.argv[1] == '-h' or sys.argv[1] == '--help'):
    print usage()
    sys.exit(0)

print reportHeader()

if len(sys.argv) == 1:
    ## print report for all stocks
    for company_code in interface.getStockList():
        stock = interface.applyRuleOnStock(company_code, 'getBuySellDecision')
        print reportBody(stock)

else:
    for company_code in sys.argv[1:] :
        ## print report for all stocks in arguments
        try:
            stock = interface.applyRuleOnStock(company_code, 'getBuySellDecision')
            print reportBody(stock)
        except Exception, err:
            print "[Error]: %s" %err.args[0]
print

